package org.nlp2rdf.navigator.client;

import java.util.Set;

import org.nlp2rdf.navigator.client.common.KnowledgeBaseDescriptorDTO;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;

public class HeadWidget extends Composite {
	
	private LoginWidget loginWidget;
	private Application app;
	private Set<KnowledgeBaseDescriptorDTO> knowledgeBaseDescriptorDTOs;
	private HorizontalPanel mainPanel;
	private Button resetButton;
	private final String headLine = "<a href=\"http://aksw.org/Projects/NKE\" >NKE Project Site</a> <br> <a href=\"http://aksw.org\" >AKSW Research Group</a>";
	
	public HeadWidget(Application app) {
		this.app = app;
		 mainPanel = new HorizontalPanel();
	}
	
	public HeadWidget(Application app,LoginWidget loginWidget) {
		this.app = app;
		 mainPanel = new HorizontalPanel();
		 this.loginWidget = loginWidget;
	}
	
	private void initialize(){
		final ListBox lb = getListBox();
		resetButton = new Button("reset");
		resetButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent arg0) {
				app.reset();
			}
		});
		lb.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				String kbId = lb.getValue(lb.getSelectedIndex());
				if(kbId.equals("none")){
					kbId = null;
				}
				app.changeKnowledgeBase(kbId);
			}
		});
		
		lb.setWidth("200px");
		
		mainPanel.add(lb);
		mainPanel.setCellHorizontalAlignment(lb,HasHorizontalAlignment.ALIGN_LEFT);
		HTML header = new HTML(headLine);
		mainPanel.add(header);
		mainPanel.setCellHorizontalAlignment(header,HasHorizontalAlignment.ALIGN_CENTER);
		if(loginWidget!=null){
			mainPanel.add(loginWidget);
			mainPanel.setCellHorizontalAlignment(loginWidget, HasHorizontalAlignment.ALIGN_RIGHT);
		}

		mainPanel.add(resetButton);
		mainPanel.setCellHorizontalAlignment(resetButton,HasHorizontalAlignment.ALIGN_RIGHT);
		mainPanel.setWidth("99%");
		mainPanel.addStyleName("module-border");
		initWidget(mainPanel);
	}
	
	
	
	public void setKnowledgeBaseDescriptorDTOs(Set<KnowledgeBaseDescriptorDTO> knowledgeBaseDescriptorDTOs) {
		this.knowledgeBaseDescriptorDTOs = knowledgeBaseDescriptorDTOs;
		initialize();
	}



	private ListBox getListBox()
	{
	    ListBox widget = new ListBox();
	    widget.addStyleName("kb-select");
	    widget.addItem("Select a knowledge base", "none");
	    for (KnowledgeBaseDescriptorDTO kb : knowledgeBaseDescriptorDTOs) {
	    	widget.addItem(kb.getKbLabel()+" - "+kb.getKbComment(), kb.getKbId());
	    	
		}
	    
//  	FOR OPTGROUP FUTURE WORK
//	    ListBox does not have an optgroup API, so you need to get the select
//	    element from your ListBox and use the DOM API to add the optgroup.
//
//	    Something like this:
//	                List<String> options = new ArrayList<String>();
//	                //omitted: fill options with the strings you want to see
//	    in the select list
//	                ListBox lb = new ListBox();
//	                SelectElement select = lb.getElement().cast();
//	                OptGroupElement groupElement =
//	    Document.get().createOptGroupElement();
//	                groupElement.setLabel("My group");
//
//	                for (String option : options) {
//	                    OptionElement optElement =
//	    Document.get().createOptionElement();
//	                    //setText results in blank options in IE6/7, use
//	    setInnerText
//	                    optElement.setInnerText(option.getName());
//	                    groupElement.appendChild(optElement);
//	                }
//
//	                select.appendChild(groupElement);
//
//	    Hope that helps, 
	    
	    return widget;
	}


}